package com.cooba.activitys.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.utils.Md5Utils;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.cooba.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

/**
 * Created by Administrator on 2015/12/16.
 * 修改电话号码
 */
public class ChangNumberActivity extends AppCompatActivity implements View.OnClickListener {
    public static final int RESULT_CODE = 1;
    private EditText et_phone;
    private EditText et_code;
    private TextView get_code;
    private EditText et_password;
    private EditText et_comfirm_password;
    private Button submit_btn;
    private ImageButton loginimage_back;
    private String phone;
    private String code;
    private String login_id;
    private String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chang_number);

        PushAgent.getInstance(this).onAppStart();

        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        get_code = (TextView) findViewById(R.id.bt_getCode);
        et_password = (EditText) findViewById(R.id.bind_num_setPwd_pwd_et);
        et_comfirm_password = (EditText) findViewById(R.id.bind_num_setPwd_comfirm_pwd_et);
        submit_btn = (Button) findViewById(R.id.submit_btn);
        loginimage_back = (ImageButton) findViewById(R.id.loginimage_back);
        get_code.setOnClickListener(this);
        submit_btn.setOnClickListener(this);
        loginimage_back.setOnClickListener(this);
        login_id = SharedPreferencesUtil.getData(this, "login_id", "");
        uuid = SharedPreferencesUtil.getData(this, "uuid", "");
    }

    @Override
    public void onClick(View v) {
        final HttpUtils httpUtils = new HttpUtils();
        final RequestParams params = new RequestParams();
        phone = et_phone.getText().toString().trim();
        code = et_code.getText().toString().trim();
        switch (v.getId()) {
            case R.id.bt_getCode:
                if (phone == null || phone.equals("")) {
                    Toast.makeText(getApplication(), "手机号码不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!Utils.isnumber(phone)) {
                    Toast.makeText(getApplication(), "手机号码格式不正确!", Toast.LENGTH_SHORT).show();
                    return;
                }
                //关闭获取
                get_code.setEnabled(false);
                //60S倒计时
                new CountDownTimer(60000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        get_code.setText((millisUntilFinished / 1000) + "秒后重发");
                    }

                    @Override
                    public void onFinish() {
                        get_code.setText("获取验证码");
                        get_code.setEnabled(true);
                    }
                }.start();
                params.addBodyParameter("phone", phone);
                httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.GetSendCode, params, new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.e("wei", "获取验证码失败");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String msg = null;
                        try {
                            msg = JSON.parseObject(responseInfo.result).getString("msg");
                            String status = JSON.parseObject(responseInfo.result).getString("status");
                            Log.e("wei", "验证码获取成功msg:" + msg + ">>status=" + status);
                            if (status.equals("0")) {
                                Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                });
                break;
            case R.id.submit_btn:
                //校验验证码
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(getApplication(), "请填写手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(getApplication(), "请填写验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(et_password.getText()) || TextUtils.isEmpty(et_comfirm_password.getText())) {
                    Toast.makeText(ChangNumberActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
                if (!et_password.getText().toString().trim().matches(regex)) {
                    Toast.makeText(ChangNumberActivity.this, "密码格式不正确!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!et_password.getText().toString().trim().equals(et_comfirm_password.getText().toString().trim())) {
                    Toast.makeText(ChangNumberActivity.this, "俩次输入的密码不一致!", Toast.LENGTH_SHORT).show();
                    return;
                }
                params.addBodyParameter("phone", phone);
                params.addBodyParameter("send_code", code);
                httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.CheckSendCode, params, new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.e("sss", "获取失败");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String status = JSON.parseObject(responseInfo.result).getString("status");
                        if (status.equals("1")) {
                            params.addBodyParameter("login_id", phone);
                            params.addBodyParameter("uuid", uuid);
                            String password = Md5Utils.encodeBy32BitMD5(et_password.getText().toString());
                            params.addBodyParameter("password",password);
                            httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.BindPhone, params, new RequestCallBack<String>() {
                                @Override
                                public void onSuccess(ResponseInfo<String> responseInfo) {
                                    Log.d("wei", "获取成功" + responseInfo.result);
                                    String status = JSON.parseObject(responseInfo.result).getString("status");
                                    String msg = JSON.parseObject(responseInfo.result).getString("msg");
                                    if (status.equals("1")) {
                                        SharedPreferencesUtil.saveData(ChangNumberActivity.this, "login_id", phone);
                                        Intent intent = new Intent();
                                        intent.putExtra("phone", phone);
                                        setResult(RESULT_OK, intent);
                                        finish();
                                    } else {
                                        Toast.makeText(ChangNumberActivity.this, msg, Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onFailure(HttpException error, String msg) {
                                    Log.d("wei", "获取失败");
                                }
                            });
                        } else {
                            Toast.makeText(getApplication(), "验证码错误!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            case R.id.loginimage_back:
                finish();
                break;
            default:
                break;
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
